// import { withConverter } from 'js-cookie'
// import { includes } from 'mock/user'
// import { login } from './api/user'
import router from './router'
import store from './store'
// import { Message } from 'element-ui'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style

import getPageTitle from '@/utils/get-page-title'
router.beforeEach(async(to, from, next) => {
  NProgress.start()// 开启进度条
  console.log(to, '到哪去')
  console.log(from, '从哪里来')
  // 动态设置网页的标题
  // document.title = '人力资源管理系统'
  document.title = getPageTitle(to.meta.title)
  // 跳转到其他页
  next()
  const token = store.state.user.token
  const whiteList = ['login', '404']
  // 判断是否有token
  if (token) {
    console.log('已经登录')
    // 判断是否前往登录页
    if (to.path === '/login') {
      console.log('已经登录不能去往登录页')
      next('/')
      NProgress.done()// 手动关闭进度条
    } else {
      console.log('已经登录去往其他页面')
      // 优化路由守卫,判断vuex中是否已经存储了用户信息,已经存储了就不用再进行获取
      if (!store.state.user.userInfo.userId) {
      // 调用dispatch
        await store.dispatch('user/getUserInfo')
      }

      next()
    }
  } else {
    console.log('没有登录')
    if (whiteList.includes(to.path)) {
      console.log('拥有白名单,直接放行')
      next()
      NProgress.done()// 手动关闭进度条
    } else {
      console.log('没有白名单,前往登录页呢')
      // next('/login')
      console.log('1')
    }
  }
})

router.afterEach(() => {
  NProgress.done()// 页面跳转完毕之后关闭进度条
})

